const sportsService = require('../service/sportsService')
const {timeFormat} = require('../utils/format')
class SportsController {
  async createSports(ctx, next) {
    const { name, time, time2 } = ctx.request.body 
    const finalTime = timeFormat(time,'YYYY-MM-DD') + ' ' + timeFormat(time2, 'HH:mm:ss')
    await sportsService.createSports(name,finalTime)
    ctx.body = {
      meta: {
        msg: '创建成功',
        status:201
      }
    }
  }
  async getAllSports(ctx, next) {
    const { pagenum, pagesize, id, name, time } = ctx.request.body
    const res = await sportsService.getAllSports(pagenum,pagesize,id,name, time )
    ctx.body = {
      data:res
    }
  }
  async getSportsById(ctx, next) {
    const {id} = ctx.params
    const res = await sportsService.getSportsById(id)
    ctx.body = {
      data:res[0]
    }
  }
  async removeSportsById(ctx, next) {
    const { id } = ctx.params 
    await sportsService.removeSportsById(id)
    ctx.body = {
      meta: {
        msg:'删除成功'
      }
    }
  }
  async updateSportsById(ctx, next) {
    const { id } = ctx.params
    const { name, time, time2 } = ctx.request.body
    const finalTime = timeFormat(time,'YYYY-MM-DD') + ' ' + timeFormat(time2, 'HH:mm:ss')
    await sportsService.updateSportsById(id, name, finalTime )
    ctx.body = {
      meta: {
        msg:'修改成功'
      }
    }
  }
  async getAllAthletes(ctx, next) {
    
  }
  
}

module.exports = new SportsController()